package com.leinao.utils.http;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
*
*
* created by wangshoufa
* 2018年8月30日 下午7:27:49
*
*/



/**
 * @author sfwang
 * @date 2017年12月4日 下午7:45:56
 * 
 */
@SuppressWarnings("deprecation")
public class HttpUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); 
	
	public static HttpResponse httpGet(String serviceUrl, String token) {
		HttpResponse response = null;
		HttpClient httpclient = new DefaultHttpClient();// 创建默认的httpClient实例.
		HttpGet httpGet = new HttpGet(serviceUrl);
		try {
			httpGet.setHeader("X-Auth-Token",token);  
			response = httpclient.execute(httpGet);// 执行方法
		} catch (ParseException e) {
			logger.info(e.getMessage(), e);
		} catch (IOException e) {
			logger.info(e.getMessage(), e);
		} finally {
			httpclient.getConnectionManager().shutdown();// 关闭连接,释放资源
		}
		return response;
	}
	
	
	public static HttpResponse httpPost(String serviceUrl, String token) {
		HttpResponse response = null;
		HttpClient httpclient = new DefaultHttpClient();// 创建默认的httpClient实例.
		HttpPost httpPost = new HttpPost(serviceUrl);
		try {
			httpPost.setHeader("X-Auth-Token",token);  
			response = httpclient.execute(httpPost);// 执行方法
		} catch (ParseException e) {
			logger.info(e.getMessage(), e);
		} catch (IOException e) {
			logger.info(e.getMessage(), e);
		} finally {
			httpclient.getConnectionManager().shutdown();// 关闭连接,释放资源
		}
		return response;
	}
	
	
	public static HttpResponse httpDelete(String serviceUrl, String token) {
		HttpResponse response = null;
		HttpClient httpclient = new DefaultHttpClient();// 创建默认的httpClient实例.
		HttpDelete httpDel = new HttpDelete(serviceUrl);
		try {
			MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
			httpDel.setHeader("X-Auth-Token",token);
			response = httpclient.execute(httpDel);// 执行方法
		} catch (ParseException e) {
			logger.info(e.getMessage(), e);
		} catch (IOException e) {
			logger.info(e.getMessage(), e);
		} finally {
			httpclient.getConnectionManager().shutdown();// 关闭连接,释放资源
		}
		return response;
	}
	
	public static HttpResponse httpPut(String serviceUrl, String token) {
		HttpResponse response = null;
		HttpClient httpclient = new DefaultHttpClient();// 创建默认的httpClient实例.
		HttpPut httpPut = new HttpPut(serviceUrl);
		try {
			httpPut.setHeader("X-Auth-Token",token);  
			response = httpclient.execute(httpPut);// 执行方法
		} catch (ParseException e) {
			logger.info(e.getMessage(), e);
		} catch (IOException e) {
			logger.info(e.getMessage(), e);
		} finally {
			httpclient.getConnectionManager().shutdown();// 关闭连接,释放资源
		}
		return response;
	}

}
